<?php

namespace app\api\controller;


class Doctor
{
    private $pagesize = 10;

    /**
     * 获取医生列表
     */
    public function lists()
    {
        $access_token = trim($_REQUEST['access_token']);
        $search = trim($_REQUEST['search']);
        $sorts = trim($_REQUEST['sorts']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 1, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array('is_delete' => 0, 'hosp_id' => $_hosp);
        if (!empty($search)) {
            $search = json_decode($search, true);
            foreach ($search as $k => $v) {
                if ($v['key']) {
                    if ($v['val'] != "") {
                        $w[$v['key']] = $v['val'];
                    }
                }
            }
        }


        $sqlSorts = "";
        if (!empty($sorts)) {
            $sorts = json_decode($sorts, true);

            foreach ($sorts as $k => $v) {
                if ($v['key']) {
                    if ($v['val'] != "") {
                        if (empty($sqlSorts)) {
                            $sqlSorts = $v['key'] . " " . $v['val'];
                        } else {
                            $sqlSorts .= "," . $v['key'] . " " . $v['val'];
                        }
                    }
                }
            }
        }

        if (empty($sqlSorts)) {
            $sqlSorts = "is_open desc,hot desc,sorts asc";
        }
        $model = new \app\admin\model\HospDoctor();
        $lists = $model->lists($w, $this->pagesize, '*', $sqlSorts);

        foreach ($lists['data'] as $key => $val) {
            $lists['data'][$key] = changeToDoctor($val, false);
        }
        $lists['hosp_info'] = $hosptialInfo;
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }


    /**
     * 根据医生uid来获取医生的评论数据
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */

    public function commentLists()
    {
        $uid = trim($_REQUEST['uid']);//医生的uid
        $search = trim($_REQUEST['search']);
        $sorts = trim($_REQUEST['sorts']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($uid)) {
            $return['err'] = 10009;
            $return['msg'] = 'uid不能为空';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $userInfo = $userModel->getDataForWhere(array('id' => $uid, 'is_delete' => 0));
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array('doctor_id' => $uid, 'stats' => 0, 'is_delete' => 0, 'hosp_id' => $_hosp);
        $sqlSorts = "id desc";
        $model = new \app\admin\model\HospComment();
        $lists = $model->lists($w, $this->pagesize, 'id,uid,contents,score,ctime', $sqlSorts);

        foreach ($lists['data'] as $key => $val) {
            $lists['data'][$key]['ctimeFormat'] = date("Y-m-d H:i:s", $val['ctime']);
        }
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    public function open()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $doctorInfo = getDoctorInfo($userInfo['id'], "*");
        if ($doctorInfo['is_open'] == 0) {
            (new \app\admin\model\HospDoctor())->updateDataForWhere(['is_open' => 1], ['id' => $doctorInfo['id']]);
        }
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        return json($return);
    }


    public function checkphone()
    {
        $phone = trim($_REQUEST['phone']);

        if (empty($phone)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = $userModel->getDataForWhere(array('uname' => $phone, 'is_delete' => 0));
        if (empty($userInfo)) {
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = 0;
            echo json_encode($return, true);
            exit;
        }
        $doctorInfo = getDoctorInfo($userInfo['id'], "*");
        if (empty($doctorInfo)) {
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = 0;
            echo json_encode($return, true);
            exit;
        }
        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = 1;//0手机号存在  1手机号不存在
        echo json_encode($return, true);
        exit;
    }


    /**
     * 医生发表说说接口
     * @return \think\response\Json
     */
    public function say()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        $say = trim($_REQUEST['say']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        (new \app\admin\model\HospDoctor())->updateDataForWhere(['say' => $say], ['uid' => $userInfo['id']]);

        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $say;
        return json($return);
    }


    /**
     * 医生接单配置
     * @return \think\response\Json
     */
    public function setting()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);

        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }


        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $doctorInfo = (new \app\admin\model\HospDoctor())->getDataForWhere(['uid' => $userInfo['id']]);
        if (empty($doctorInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $fast_service_price_now = trim($_REQUEST['fast_service_price_now']);
        if (empty($fast_service_price_now)) {
            $fast_service_price_now = 0;
        }

        if ($fast_service_price_now <= 0 || $fast_service_price_now >= 1000) {
            $return['err'] = 10009;
            $return['msg'] = '价格范围【1-1000】之间';
            echo json_encode($return, true);
            exit;
        }
        $fast_service_price = trim($_REQUEST['fast_service_price']);
        if (empty($fast_service_price)) {
            $fast_service_price = 0;
        }
        if ($fast_service_price <= 0 || $fast_service_price >= 1000) {
            $return['err'] = 10009;
            $return['msg'] = '价格范围【1-1000】之间';
            echo json_encode($return, true);
            exit;
        }

        $service_price = trim($_REQUEST['service_price']);
        if (empty($service_price)) {
            $service_price = 0;
        }
        if ($service_price <= 0 || $service_price >= 1000) {
            $return['err'] = 10009;
            $return['msg'] = '价格范围【1-1000】之间';
            echo json_encode($return, true);
            exit;
        }

        $service_price_now = trim($_REQUEST['service_price_now']);
        if (empty($service_price_now)) {
            $service_price_now = 0;
        }

        if ($service_price_now <= 0 || $service_price_now >= 1000) {
            $return['err'] = 10009;
            $return['msg'] = '价格范围【1-1000】之间';
            echo json_encode($return, true);
            exit;
        }

        $is_open = trim($_REQUEST['is_open']);
        if (empty($is_open)) {
            $is_open = 0;
        }
        if (!in_array($is_open, [0, 1])) {
            $return['err'] = 1;
            $return['msg'] = '参数错误，is_open';
            return $return;
        }
        $is_open_fast = trim($_REQUEST['is_open_fast']);
        if (empty($is_open_fast)) {
            $is_open_fast = 0;
        }
        if (!in_array($is_open_fast, [0, 1])) {
            $return['err'] = 1;
            $return['msg'] = '参数错误，is_open_fast';
            return $return;
        }

        $is_open_multi = trim($_REQUEST['is_open_multi']);
        if (empty($is_open_multi)) {
            $is_open_multi = 0;
        }

        if (!in_array($is_open_multi, [0, 1])) {
            $return['err'] = 1;
            $return['msg'] = '参数错误，is_open_multi';
            return $return;
        }
        $start_msg = trim($_REQUEST['start_msg']);
        if (empty($start_msg)) {
            $start_msg = "";
        }
        $end_msg = trim($_REQUEST['end_msg']);
        if (empty($end_msg)) {
            $end_msg = "";
        }
        $upData = array(
            'fast_service_price_now' => $fast_service_price_now,
            'fast_service_price' => $fast_service_price,
            'service_price' => $service_price,
            'service_price_now' => $service_price_now,
            'is_open_fast' => $is_open_fast,
            'is_open_multi' => $is_open_multi,
            'is_open' => $is_open,
            'start_msg' => $start_msg,
            'end_msg' => $end_msg,
        );

        (new \app\admin\model\HospDoctor())->updateDataForWhere($upData, ['id' => $doctorInfo['id']]);
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $upData;
        return json($return);
    }


    public function close()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $doctorInfo = getDoctorInfo($userInfo['id'], "*");
        if ($doctorInfo['is_open'] == 1) {
            (new \app\admin\model\HospDoctor())->updateDataForWhere(['is_open' => 0], ['id' => $doctorInfo['id']]);
        }
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        return json($return);
    }


    public function tixian()
    {
        $access_token = trim($_REQUEST['access_token']);
        $money = trim($_REQUEST['money']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $money = roundingE($money, 2);
        if ($money < 1) {
            $return['err'] = 10009;
            $return['msg'] = '每次提现金额不能小于1元';
            echo json_encode($return, true);
            exit;
        }
        if ($userInfo['money'] < $money) {
            $return['err'] = 10009;
            $return['msg'] = '余额不足';
            echo json_encode($return, true);
            exit;
        }

        $data = array(
            'uid' => $userInfo['id'],
            'zfb_name' => $userInfo['alipay_name'],
            'zfb_account' => $userInfo['alipay_account'],
            'tx_type' => 0,//0提现到支付宝  1提现到微信
            'ctime' => time(),
            'stats' => 0,
            'money' => $money,
            'source' => 1
        );
        //写一个事物来扣款
        $result = doctorMoneyTxApply($data);
        if ($result['err'] == 0) {
            $return = ['err' => 0, 'msg' => '操作成功', 'devMsg' => '操作成功'];
            return json($return);
        } else {
            return json($result);
        }

        $return['err'] = 0;
        $return['msg'] = '操作成功';
        return json($return);
    }


    public function txLists()
    {
        $access_token = trim($_REQUEST['access_token']);
        $money = trim($_REQUEST['money']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 2, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array();
        $w['uid'] = $userInfo['id'];
        $w['is_delete'] = 0;
        $w['source'] = 1;
        $model = new \app\admin\model\Txapply();
        $lists = $model->lists($w, $this->pagesize);

        $lists['data'] = \app\admin\mapper\Txapply::getInstance()->listMapper($lists['data']);
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $lists;
        return json($return);
    }
}

